﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Unis.Framework.ORM.Attributes;
using Unis.Framework.RuleBase.Common;
using KingRoad.EPM.DeviceEntity.Model;
using KingRoad.EPM.DeviceDAL;
using System.Data;

namespace KingRoad.EPM.DeviceDAL.BllRoadwayService
{
    /// <summary> 
    /// 文件生成时间 2018/4/16 17:13:34
    /// </summary> 
    public class BllRoadwayService : BaseService<BllRoadwayInfo>, IBllRoadwayService
    {
    private readonly string _tableName = "";
        //SqlConfigFile文件中的文件名称
        private readonly string _modelMapXmlName = "";
        //映射的表的主键
        private readonly string _modelMapPrimaryKey = "";

        public BllRoadwayService()
        {
            var modelAttri = typeof(BllRoadwayInfo).GetCustomAttributes(typeof(ModelMapAttribute), false).FirstOrDefault() as ModelMapAttribute;
            _modelMapXmlName = modelAttri != null ? modelAttri.XmlFileName : "";
            _tableName = modelAttri != null ? modelAttri.TableName : "";
            if (!string.IsNullOrEmpty(_modelMapXmlName))
            {
                var arrayName = _modelMapXmlName.Split('_').ToList();
                var tempXmlName = "";
                arrayName.ForEach(t =>
                {
                    tempXmlName += t.Substring(0, 1).ToUpper() + t.Substring(1).ToLower();
                });
                _modelMapXmlName = tempXmlName;
            }



            _modelMapPrimaryKey = modelAttri != null ? modelAttri.PrimaryKeys[0] : "";
        }

        public List<BllRoadwayInfo> GetRoadwayEntityListByContionRe(int numCurrentPage, int numPerPage, string orderByField, out int totalRecord, string condition = " 1=1 ", string desc = "desc", string table = "")
        {
            if (table == "")
                table = _tableName;
            if (String.IsNullOrEmpty(_modelMapXmlName))
            {
              
                totalRecord = 0; return null;
            }

              table = " V_Roadway_Meter  ";
            if (String.IsNullOrEmpty(condition))
                condition = " 1=1 ";
            //检索数据的总条数
            totalRecord = DatabaseExcuteBySql.ExecuteScalar<int>("SELECT COUNT(1) FROM  " + table + " TT WHERE " + condition + "");


            var startNum = (numCurrentPage - 1) * numPerPage + 1;
            var endNum = numCurrentPage * numPerPage >= totalRecord ? totalRecord : numCurrentPage * numPerPage;


            var sqlStr = string.Format(@"SELECT  *
                                         FROM    (SELECT * ,ROW_NUMBER() OVER ( ORDER BY {0} {5} ) AS RowNumber
                                                 FROM  {1} AS a WHERE {2}) AS b
                                         WHERE   b.RowNumber BETWEEN {3} AND {4};", "a." + orderByField, table, condition, startNum, endNum, desc);

            var splitList = DatabaseExcuteBySql.QueryBySql<BllRoadwayInfo>(sqlStr);

            return splitList;
        }
        public DataTable GetMeterStateTotalBySql(string sql) 
        {

            var sqlStr = "Select count(id) as Total,ISNULL(sum( case when state=0 and DateDiff(second,ReportTime,getdate())<3600  then 1 else 0 end),0) as ZTotal,ISNULL(sum( case when state<>0 or DateDiff(second,ReportTime,getdate())>3600 then 1 else 0 end),0) as YTotal   FROM V_Roadway_Meter where " + sql;

            var dt = DatabaseExcuteBySql.QuerybySql(sqlStr);

            return dt;
        }
      
    }

}
